import 'package:bot_toast/bot_toast.dart';
// ignore: implementation_imports
import 'package:bot_toast/src/toast_widget/animation.dart';
import 'package:trollee/app/core/core.dart';

class AppToast extends StatelessWidget {
  const AppToast({
    required this.text,
    required this.cancelFunc,
    Key? key,
    this.title,
    this.icon,
    this.iconColor,
    this.actionText,
    this.onActionClick,
  }) : super(key: key);

  final IconData? icon;
  final Color? iconColor;
  final String text;
  final String? title;
  final String? actionText;
  final VoidCallback? onActionClick;
  final void Function() cancelFunc;

  @override
  Widget build(BuildContext context) {
    return Card(
      color: AppColors.font_color_primary.withAlpha(232),
      shape: RoundedRectangleBorder(
        borderRadius: AppRadii.k10pxRadius,
      ),
      margin: EdgeInsets.symmetric(horizontal: AppDimens.dp8),
      child: ConstrainedBox(
        constraints: BoxConstraints(minHeight: AppDimens.dp56),
        child: Padding(
          padding: EdgeInsets.all(AppDimens.dp16),
          child: Stack(
            children: [
              Row(
                children: <Widget>[
                  _buildIcon(
                    title.isNotNullAndNotBlank ? Colors.transparent : null,
                  ),
                  _buildTitleContent(),
                  _buildAction(),
                ],
              ),
              if (title.isNotNullAndNotBlank) _buildIcon(null),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildIcon(Color? color) {
    if (icon == null) {
      return const SizedBox.shrink();
    }
    return Icon(
      icon,
      size: AppDimens.dp24,
      color: color ?? iconColor ?? Colors.white,
    ).marginOnly(right: AppDimens.dp8);
  }

  Widget _buildTitleContent() {
    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          if (title.isNotNullAndNotBlank)
            Text(
              title!,
              style: AppTextStyles.body1_14_Bold_primary.color_font_color_fifth,
            ),
          if (title.isNotNullAndNotBlank) SizedBox(height: AppDimens.dp2),
          Text(
            text,
            style: AppTextStyles.body2_12_Regular_primary.copyWith(
              color: AppColors.font_color_while,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildAction() {
    if (!actionText.isNotNullAndNotBlank) {
      return const SizedBox.shrink();
    }
    return TextButton(
      onPressed: () {
        cancelFunc.call();
        onActionClick?.call();
      },
      style: ButtonStyle(
        shape: MaterialStateProperty.all(RoundedRectangleBorder(
            borderRadius: AppRadii.k8pxRadius,
            side: const BorderSide(color: Colors.white))),
        padding: MaterialStateProperty.all(EdgeInsets.symmetric(
          vertical: AppDimens.dp8,
          horizontal: AppDimens.dp16,
        )),
      ),
      child: Text(
        actionText!,
        style:
            AppTextStyles.body2_12_Bold_primary.color_font_color_fifth.line_height1_2,
      ),
    ).marginOnly(left: AppDimens.dp8);
  }

  static void show(
    String text, {
    String? title,
    Duration? duration = const Duration(seconds: 2),
    IconData? icon = AppIcons.general_ic_info,
    Color? iconColor = Colors.white,
    String? actionText,
    VoidCallback? onActionClick,
    VoidCallback? onClose,
    bool crossPage = true,
    Alignment alignment = const Alignment(0, 0.8),
  }) {
    if (text.isEmpty) return;
    BotToast.showCustomNotification(
      crossPage: crossPage,
      onClose: onClose,
      dismissDirections: const [
        DismissDirection.horizontal,
        DismissDirection.down
      ],
      duration: duration,
      wrapToastAnimation: textAnimation,
      align: alignment,
      toastBuilder: (void Function() cancelFunc) {
        return AppToast(
          text: text,
          title: title,
          icon: icon,
          iconColor: iconColor,
          actionText: actionText,
          onActionClick: onActionClick,
          cancelFunc: cancelFunc,
        );
      },
    );
  }

  static void showError(
    String text, {
    String? title,
    Duration? duration = const Duration(seconds: 2),
    String? actionText,
    VoidCallback? onActionClick,
    VoidCallback? onClose,
    bool crossPage = true,
    Alignment alignment = const Alignment(0, 0.8),
  }) {
    return show(
      text,
      title: title,
      duration: duration,
      icon: AppIcons.general_ic_fail,
      iconColor: AppColors.meaning_color_danger,
      actionText: actionText,
      onActionClick: onActionClick,
      onClose: onClose,
      crossPage: crossPage,
      alignment: alignment,
    );
  }

  static void showSuccess(
    String text, {
    String? title,
    Duration? duration = const Duration(seconds: 2),
    String? actionText,
    VoidCallback? onActionClick,
    VoidCallback? onClose,
    bool crossPage = true,
    Alignment alignment = const Alignment(0, 0.8),
  }) {
    return show(
      text,
      title: title,
      duration: duration,
      icon: AppIcons.general_ic_successful,
      iconColor: AppColors.meaning_color_success,
      actionText: actionText,
      onActionClick: onActionClick,
      onClose: onClose,
      crossPage: crossPage,
      alignment: alignment,
    );
  }

  static void showWarning(
    String text, {
    String? title,
    Duration? duration = const Duration(seconds: 2),
    String? actionText,
    VoidCallback? onActionClick,
    VoidCallback? onClose,
    bool crossPage = true,
    Alignment alignment = const Alignment(0, 0.8),
  }) {
    return show(
      text,
      title: title,
      duration: duration,
      icon: AppIcons.general_ic_warning,
      iconColor: const Color(0xFFFA8442),
      actionText: actionText,
      onActionClick: onActionClick,
      onClose: onClose,
      crossPage: crossPage,
      alignment: alignment,
    );
  }
}
